package com.tuananh.radnom.persist.imp.xml;

import java.util.ArrayList;

import com.ajexperience.utils.DeepCopyException;
import com.ajexperience.utils.DeepCopyUtil;
import com.tuananh.radnom.model.NetElementConfiguration;
import com.tuananh.radnom.model.NetInterface;
import com.tuananh.radnom.model.NetNode;
import com.tuananh.radnom.model.event.EventReporter;
import com.tuananh.radnom.model.event.NeEvent;
import com.tuananh.radnom.model.event.NmsEvent;
import com.tuananh.radnom.model.netpoller.PollingService;
import com.tuananh.radnom.model.netservice.NetService;
import com.tuananh.radnom.persist.INetNodeDAO;

public class NetNodeXmlDAO extends XmlDAO implements INetNodeDAO {
	NetElementConfiguration netConfig;
	PollingService pollService;

	public NetNodeXmlDAO() {
		super(NetElementConfiguration.path);
		configuration = XmlConfigHolder.neConfig;
		try {
			savedConfiguration = (new DeepCopyUtil()).deepCopy(configuration);
		} catch (DeepCopyException e) {
			// TODO Auto-generated catch block
			savedConfiguration = new NetElementConfiguration();
			e.printStackTrace();
		}
		netConfig = (NetElementConfiguration) configuration;
	}

	public String getName() {
		return "NetNodeEvent";
	}

	public ArrayList<NetNode> getAll() {
		// TODO Auto-generated method stub
		try {
			return netConfig.getListNode();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	protected void reportEvent(Object netNode, String methodName) {
		NeEvent event = new NeEvent(methodName, (NetNode) netNode);

		EventReporter.report(event);
	}

	public boolean add(NetNode netNode) {
		// TODO Auto-generated method stub
		if (getByName(netNode.getName()) == null) {
			for (NetNode item : netConfig.getListNode()) {
				if (!item.validate(netNode))
					return false;
			}
			netConfig.getListNode().add(netNode);
			reportEvent(netNode, "add");
			return true;
		}
		return false;
	}

	public boolean update(NetNode netNode) {
		// TODO Auto-generated method stub
		ArrayList<NetNode> list = netConfig.getListNode();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getName().equals(netNode.getName())) {
				list.set(i, netNode);
				reportEvent(netNode, "update");
				return true;
			}
		}
		return false;
	}

	public boolean remove(String name) {
		// TODO Auto-generated method stub
		ArrayList<NetNode> list = netConfig.getListNode();
		for (NetNode node : list)
			if (node.getName().equals(name)) {
				reportEvent(node, "remove");
				list.remove(node);
				return true;
			}
		return false;
	}

	public NetNode getByName(String name) {
		// TODO Auto-generated method stub
		for (NetNode item : netConfig.getListNode()) {
			if (item.getName().equals(name))
				try {
					return item;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					break;
				}
		}
		return null;
	}

	public NetNode findByIntName(String name) {
		// TODO Auto-generated method stub
		for (NetNode item : netConfig.getListNode()) {
			for (NetInterface netInt : item.getNetInterfaces())
				if (netInt.getName().equals(name))
					return item;
		}
		return null;
	}

	public ArrayList<NetNode> findByIntName(ArrayList<String> names) {
		// TODO Auto-generated method stub
		return null;
	}

	public ArrayList<String> getAllNodeName() {
		// TODO Auto-generated method stub
		ArrayList<String> nameList = new ArrayList<String>();
		for (NetNode node : netConfig.getListNode()) {
			nameList.add(node.getName());
		}
		return nameList;
	}


	public boolean updateInfo(NetNode resource) {
		// TODO Auto-generated method stub
		return false;
	}

	boolean contain(ArrayList<NetInterface> list, String netIntName) {
		for (NetInterface netInt : list)
			if (netInt.getName().equals(netIntName))
				return true;
		return false;
	}


	public boolean merge(NetNode first, NetNode second) {
		// TODO Auto-generated method stub
		if (first == null || second == null)
			return false;
		if (second.getDisplayName() != null && !second.getDisplayName().contains("AutoGenerate"))
		if (second.getDisplayName() != null)
			if (first.getDisplayName().contains("AutoGenerate"))
				first.setDisplayName(second.getDisplayName());
		if (second.getTechInfo() != null)
			first.getTechInfo().putAll(second.getTechInfo());
		for (NetInterface netInt : second.getNetInterfaces()) {
			if (!contain(first.getNetInterfaces(), netInt.getName())) {
				first.getNetInterfaces().add(netInt);
			}
		}
		return false;
	}


	public NetNode findByIntIp(String ip) {
		// TODO Auto-generated method stub
		if (ip == null)
			return null;
		ArrayList<NetNode> listNode = getAll();
		for (NetNode node : listNode) {
			for (NetInterface netInt : node.getNetInterfaces())
				if (ip.equals(netInt.getIpAddress()))
					return node;
		}
		return null;
	}
}
